package com.mon4cc.simulation;

import lombok.extern.slf4j.Slf4j;
import org.jbpt.pm.FlowNode;
import org.springframework.util.Assert;
import org.springframework.util.ObjectUtils;

import java.util.Map;

/**
 * @author Ping
 * @date 2022/12/19 11:34
 **/
@Slf4j
public class ActivitySimulatedInstance extends FlowNodeSimulatedInstance {
    public ActivitySimulatedInstance(TokenBasedBPMNSimulation bpmnSimulation, FlowNode flowNode) {
        super(bpmnSimulation, flowNode);
    }

    @Override
    public void doSimulate(ExecuteContext context) {
        // check if it involved in swapped work
        // check if it involved in artificial work
        if (context.needToAddArtificalInstance(this)) {
            writer.write(SimulationConfig.Art_Event_Name, context) ;
        }
        if (context.needToSwapped(this)) {
           this.rename(context.getSwappedInstanceName(this));
        }
/*
        try {
            Thread.sleep(200);
        } catch (Exception e) {
            e.printStackTrace();
        }*/
        // if it had write data objects, then get simulated values for these data objects.
        if (! ObjectUtils.isEmpty(this.flowNode.getWriteDocuments())) {
            Map<String, Object> writeDataValues = this.bpmnSimulation.getSimulatedValueForDataObject(this.flowNode);

            writer.write(context, this, writeDataValues);
            if (! ObjectUtils.isEmpty(writeDataValues)) {
                context.updateDataContext(writeDataValues);
            }
        } else {
            writer.write(this,context);
        }

    }

}
